<%
   new_prefix = normal_method_name(sprintf("%s_%s", prefix, property.out_name))
   func_def = sprintf("expand_%s(d, array_index):", new_prefix)
-%>
<% if !property.to_request.nil? -%>


def <%= func_def %>
<%= indent(lines(property.to_request, -1), 4) -%>
<% elsif property.is_a?(Api::Type::Array) && property.item_type.is_a?(Api::Type::NestedObject) -%>
<%   no_do = property.array_num.nil? -%>
<%   if no_do -%>
<%     no_do = property.field.nil? || property.field.empty? -%>
<%     unless no_do -%>
<%       rp = resource.find_property(property.field) -%>
<%       no_do = rp.nil? || rp.crud.eql?("r") -%>
<%     end -%>
<%     unless no_do || rp.is_a?(Api::Type::Array) -%>
<%       raise "list api parameter(#{property.name}) is an array, but the matched property is not" -%>
<%     end -%>
<%   end -%>
<%   unless no_do -%>


def <%= func_def %>
    new_ai = dict()
    if array_index:
        new_ai.update(array_index)

    req = []

<%   if property.array_num.nil? -%>
    v = navigate_value(d, [<%= index2navigate(property.field, true) %>],
                       new_ai)

    n = len(v) if v else 1<%# v may be None %>
<%   else -%>
    n = <%= property.array_num %>
<%   end -%>
    for i in range(n):
<%   unless property.field.nil? -%>
        new_ai["<%= to_schema_index(property.field) %>"] = i
<%   end -%>
        transformed = dict()
<%     nested_properties = nested_properties(property) -%>
<%       nested_properties.each do |prop| -%>

<%= indent(convert_list_api_parameter(resource, prop, "d, new_ai", new_prefix, "transformed", 8), 8) %>
<%       end -%>

        for _, v in transformed.items():
            if v is not None:
                req.append(transformed)
                break

    return req if req else None
<%     nested_properties.each do |prop| -%>
<%= lines(build_expand_list_api_method(resource, new_prefix, prop)) -%>
<%     end -%>
<%   end # no_do -%>
<% elsif property.is_a?(Api::Type::NestedObject) -%>


def <%= func_def %>
    r = dict()
<%   nested_properties = nested_properties(property) -%>
<%     nested_properties.each do |prop| -%>

<%= indent(convert_list_api_parameter(resource, prop, "d, array_index", new_prefix, "r", 4), 4) %>
<%     end -%>

    for _, v in r.items():
        if v is not None:
            return r
    return None
<%   nested_properties.each do |prop| -%>
<%= lines(build_expand_list_api_method(resource, new_prefix, prop)) -%>
<%   end -%>
<% end -%>
